package Entities;

import java.io.InputStream;
import java.sql.*;
import javax.xml.stream.*;

import com.sun.xml.internal.ws.util.xml.StAXResult;


public class SQLXMLDB {
	Connection connection;

	public void getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			String url = "jdbc:mysql://localhost/tootList";

			connection = DriverManager.getConnection(url, "root", "");

			DatabaseMetaData metadata = connection.getMetaData();
			ResultSet rs = metadata.getTypeInfo();
			rs.next();

			while (rs.next()) {
				System.out.println("TYPE_NAME:" + rs.getString("TYPE_NAME"));

			}
		} catch (SQLException e) {System.out.println(e.getMessage());
		} catch (ClassNotFoundException e) {System.out.println(e.getMessage());
		}
	}


	public void getXMLDocument() throws XMLStreamException{

		try{
			PreparedStatement stmt = connection.prepareStatement("Select * from model where idModel = 1");
			ResultSet rs= stmt.executeQuery();

			SQLXML sqlXML = rs.getSQLXML("libelle_fr");
			System.out.println(sqlXML.getString());
			InputStream binaryStream = sqlXML.getBinaryStream();
			XMLInputFactory factory = XMLInputFactory.newInstance();
			XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(binaryStream);

			parseXMLDocument(xmlStreamReader);
			sqlXML.free();

		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	public void parseXMLDocument(XMLStreamReader xmlStreamReader){

		try{
			while(xmlStreamReader.hasNext()){
				int parseEvent=xmlStreamReader.next();
				if(parseEvent==XMLStreamConstants.ATTRIBUTE){
					System.out.println("Parse Event ATTRIBUTE");
					System.out.println("Attribute Local Name: "+xmlStreamReader.getAttributeLocalName(0));
					System.out.println("Attribute Namespace: "+xmlStreamReader.getAttributeNamespace(0));
					System.out.println("Attribute Prefix: "+xmlStreamReader.getAttributePrefix(0));
					System.out.println("Attribute Type: "+xmlStreamReader.getAttributeType(0));
					System.out.println("Attribute Value: "+xmlStreamReader.getAttributeValue(0));

				}
				if(parseEvent==XMLStreamConstants.CDATA){
					System.out.println("Parse Event CDATA");
					System.out.println("CDATA text: "+xmlStreamReader.getText());
				}
				if(parseEvent==XMLStreamConstants.CHARACTERS){
					System.out.println("Parse Event CHARACTERS");
					System.out.println("CHARACTERS text: "+xmlStreamReader.getText());
				}
				if(parseEvent==XMLStreamConstants.COMMENT){
					System.out.println("Parse Event COMMENT");
					System.out.println("COMMENT text: "+xmlStreamReader.getText());
				}
				if(parseEvent==XMLStreamConstants.NOTATION_DECLARATION){
					System.out.println("Parse Event NOTATION_DECLARATION");
				}
				if(parseEvent==XMLStreamConstants.START_DOCUMENT){
					System.out.println("Parse Event START_DOCUMENT");
				}
				if(parseEvent==XMLStreamConstants.START_ELEMENT){
					System.out.println("Parse Event START_ELEMENT");
					System.out.println("Element Local Name: "+xmlStreamReader.getLocalName());
					System.out.println("Element text: "+xmlStreamReader.getElementText());
					System.out.println("Element Prefix: "+xmlStreamReader.getPrefix());
					System.out.println("Element Namespace: "+xmlStreamReader.getNamespaceURI());
				}
				if(parseEvent==XMLStreamConstants.END_ELEMENT){
					System.out.println("Parse Event END_ELEMENT");
					System.out.println("Element Local Name: "+xmlStreamReader.getLocalName());
				}
				if(parseEvent==XMLStreamConstants.ENTITY_DECLARATION){
					System.out.println("Parse Event ENTITY_DECLARATION");
				}
				if(parseEvent==XMLStreamConstants.ENTITY_REFERENCE){
					System.out.println("Parse Event ENTITY_REFERENCE");
					System.out.println("Replacement text: "+xmlStreamReader.getElementText());
				}
				if(parseEvent==XMLStreamConstants.NAMESPACE){
					System.out.println("Parse Event NAMESPACE");
					System.out.println("Namespace Prefix: "+xmlStreamReader.getNamespacePrefix(0));
					System.out.println("NamespaceURI: "+xmlStreamReader.getNamespaceURI(0));

				}
				if(parseEvent==XMLStreamConstants.SPACE){
					System.out.println("Parse Event SPACE");
					System.out.println("SPACE text: "+xmlStreamReader.getText());

				}
				if(parseEvent==XMLStreamConstants.END_DOCUMENT){
					System.out.println("Parse Event END_DOCUMENT");
				}
				if(parseEvent==XMLStreamConstants.DTD){
					System.out.println("Parse Event DTD");
				}

			}

		}
		catch(XMLStreamException e){}

	}
}

